package com.geh.learn.completablefuture;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.function.BiConsumer;

public class AnyOf {

    public static void main(String[] args) throws InterruptedException, ExecutionException {
        CompletableFuture<String> f1 = CompletableFuture.supplyAsync(() -> {
            try {
                TimeUnit.SECONDS.sleep(13);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            return "f1";
        });

//        f1.whenCompleteAsync(new BiConsumer<String, Throwable>() {
//            @Override
//            public void accept(String s, Throwable throwable) {
//                System.out.println(System.currentTimeMillis() + ":" + s);
//            }
//        });

        CompletableFuture<String> f2 = CompletableFuture.supplyAsync(() -> {
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            return "f2";
        });

//        f2.whenCompleteAsync(new BiConsumer<String, Throwable>() {
//            @Override
//            public void accept(String s, Throwable throwable) {
//                System.out.println(System.currentTimeMillis() + ":" + s);
//            }
//        });

        CompletableFuture<Object> anyOf = CompletableFuture.anyOf(f1, f2);
        System.out.println("到这里了。");
        long start = System.currentTimeMillis();
        anyOf.get();
        System.out.println(System.currentTimeMillis() - start);
        TimeUnit.SECONDS.sleep(15);
    }

}
